﻿//-----------------------------------------------------------------------
// <copyright file="WorkerViewModel.cs" company="FER">
//     Copyright (c) All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace NMBP.ViewModels
{
    using System;
    using System.Collections.Generic;

    using AutoMapper;

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    using NMBP.DAL;
    using NMBP.Models;

    /// <summary>
    /// Represents view model for WorkerWindow
    /// </summary>
    public class WorkerViewModel : ViewModelBase
    {
        #region Bindable fields

        /// <summary>
        /// Selected order in pending orders
        /// </summary>
        private DataGridOrder selectedPendingOrder;

        /// <summary>
        /// Selected order in old orders
        /// </summary>
        private DataGridOrder selectedOldOrder;

        /// <summary>
        /// List of pending orders
        /// </summary>
        private List<DataGridOrder> pendingOrders;

        /// <summary>
        /// List of old orders
        /// </summary>
        private List<DataGridOrder> oldOrders;

        /// <summary>
        /// List of order products
        /// </summary>
        private List<Product> selectedOrderProducts;

        /// <summary>
        /// Action raised when view model needs to be initialized
        /// </summary>
        private Action<Worker> initWorkerViewModel;
        #endregion

        /// <summary>
        /// Worker recieved from login screen
        /// </summary>
        private Worker recievedWorker;

        /// <summary>
        /// Initializes a new instance of the WorkerViewModel class
        /// </summary>
        public WorkerViewModel()
        {
            this.initWorkerViewModel = new Action<Worker>(this.InitializeViewModel);
            Messenger.Default.Register<Worker>(this, "InitializeWorkerWindow", this.initWorkerViewModel);

            this.FinishOrder = new RelayCommand(
                () =>
                    {
                        // TODO
                    });
        }

        #region Binding commands
        /// <summary>
        /// Gets the command raised when finish pending order button is clicked
        /// </summary>
        public RelayCommand FinishOrder { get; private set; }
        #endregion

        #region Bindable properties
        
        /// <summary>
        /// Gets or sets dialog service
        /// </summary>
        public IDialogService DialogService { get; set; }

        /// <summary>
        /// Gets or sets db facade
        /// </summary>
        public DbFacade DbFacade { get; set; }

        /// <summary>
        /// Gets or sets  Selected order in pending orders
        /// </summary>
        public DataGridOrder SelectedPendingOrder
        {
            get
            {
                return this.selectedPendingOrder;
            }

            set
            {
                this.selectedPendingOrder = value;
                this.SelectedOrderProducts.Clear();
                this.SelectedOrderProducts.AddRange(value.Cart.Products);
                this.RaisePropertyChanged("SelectedPendingOrder");
            }
        }

        /// <summary>
        /// Gets or sets Selected order in old orders
        /// </summary>
        public DataGridOrder SelectedOldOrder
        {
            get
            {
                return this.selectedOldOrder;
            }

            set
            {
                this.selectedOldOrder = value;
                this.RaisePropertyChanged("SelectedOldOrder");
            }
        }

        /// <summary>
        /// Gets or sets List of pending orders
        /// </summary>
        public List<DataGridOrder> PendingOrders
        {
            get
            {
                return this.pendingOrders;
            }

            set
            {
                this.pendingOrders = value;
                this.RaisePropertyChanged("PendingOrders");
            }
        }

        /// <summary>
        /// Gets or sets List of old orders
        /// </summary>
        public List<DataGridOrder> OldOrders
        {
            get
            {
                return this.oldOrders;
            }

            set
            {
                this.oldOrders = value;
                this.RaisePropertyChanged("OldOrders");
            }
        }

        /// <summary>
        /// List of order products
        /// </summary>
        public List<Product> SelectedOrderProducts
        {
            get
            {
                return this.selectedOrderProducts;
            }
            set
            {
                this.selectedOrderProducts = value;
                this.RaisePropertyChanged("SelectedOrderProducts");
            }
        }

        #endregion

        /// <summary>
        /// Initializes view model
        /// </summary>
        /// <param name="worker">Worker for initialization</param>
        private void InitializeViewModel(Worker worker)
        {
            if (this.DbFacade == null)
            {
                this.DbFacade = new DbFacade();
            }
            this.DialogService = new DialogService();
            this.SelectedOrderProducts = new List<Product>();

            this.recievedWorker = worker;

            this.LoadPendingOrders();
            this.LoadOldOrders();
        }

        /// <summary>
        /// Loads pending orders
        /// </summary>
        private void LoadPendingOrders()
        {
            var pendingOrdersFromDb = this.DbFacade.GetPendingOrders();
            foreach (var order in pendingOrdersFromDb)
            {
                var dataGridOrder = new DataGridOrder();

                dataGridOrder.AvailableSuppliers = this.DbFacade.GetSuppliers();
                dataGridOrder.SelectedSupplier = dataGridOrder.AvailableSuppliers[0];

                Mapper.CreateMap<Order, DataGridOrder>();
                Mapper.Map(order, dataGridOrder);

                this.PendingOrders.Add(dataGridOrder);
            }
        }

        /// <summary>
        /// Loads old orders
        /// </summary>
        private void LoadOldOrders()
        {
            var oldOrdersFromDb = this.DbFacade.GetOldOrders();
            foreach (var order in oldOrdersFromDb)
            {
                var dataGridOrder = new DataGridOrder();
                Mapper.CreateMap<Order, DataGridOrder>();
                Mapper.Map(order, dataGridOrder);

                this.OldOrders.Add(dataGridOrder);
            }
        }
    }
}
